package com.yantong.controller;

import com.yantong.pojo.Industry;
import com.yantong.pojo.IndustryBrand;
import com.yantong.service.IndustryBrandService;
import com.yantong.service.IndustryService;
import com.yantong.annotation.Log;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 *  品牌IndustryBrandController
 *
 * @author huangshuai-728982210@qq.com
 * @date 2021/6/8 17:10
 */
@Controller
@RequestMapping("/centre/industryBrand")
public class IndustryBrandController extends BaseController {

	@Autowired
	private IndustryBrandService industryBrandService;
	@Autowired
	private IndustryService industryService;

	private static final String prefix = "centre/industryBrand";


	/**
	 * 页面
	 *
	 * @return
	 */
	@GetMapping("/industryBrand")
	@RequiresPermissions("centre:industryBrand:industryBrand")
	public String industryBrand(Model model) {

		return prefix + "/industryBrand";
	}


	/**
	 * 列表数据
	 *
	 * @param industryBrand
	 * @param offset
	 * @param limit
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("centre:industryBrand:industryBrand")
	@ResponseBody
	public PageUtils<IndustryBrand> listIndustryBrand(IndustryBrand industryBrand, int offset, int limit) {
		industryBrand.setCentreId(getUserCentreId());
		return industryBrandService.listIndustryBrand(industryBrand, offset, limit);
	}

	/**
	 * 编辑页面
	 * @param industryBrandId
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{industryBrandId}")
	@RequiresPermissions("centre:industryBrand:edit")
	public String edit(@PathVariable Long industryBrandId, Model model) {
		HashMap hashMap = new HashMap();
		hashMap.put("centreId",getUserCentreId());
		model.addAttribute("industrys", industryService.selectByList(hashMap));
		model.addAttribute("industryBrand", industryBrandService.getIndustryBrandById(industryBrandId));
		return prefix + "/edit";
	}

	/**
	 * 编辑操作
	 * @param industryBrand
	 * @return
	 */
	@Log(module = "品牌管理", operation = "编辑品牌")
	@ResponseBody
	@RequiresPermissions("centre:industryBrand:edit")
	@PostMapping("/update")
	public ResultVo update(IndustryBrand industryBrand) {
		if (industryBrand == null || industryBrand.getId() == null) {
			return ResultVo.error();
		}
		Industry industry = industryService.getIndustryById(industryBrand.getIndustryId());
		industryBrand.setIndustryName(industry.getName());
		industryBrand.setCentreId(getUserCentreId());
		if (industryBrandService.updateIndustryBrand(industryBrand) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	@RequiresPermissions("centre:industryBrand:add")
	@GetMapping("/add")
	public String add(Model model) {
		HashMap hashMap = new HashMap();
		hashMap.put("centreId",getUserCentreId());
		model.addAttribute("industrys", industryService.selectByList(hashMap));
		return prefix +"/add";
	}


	@Log(module = "品牌管理", operation = "添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("centre:industryBrand:add")
	public ResultVo saveRate(IndustryBrand industryBrand) {
		if (industryBrand == null) {
			return ResultVo.error();
		}
		Industry industry = industryService.getIndustryById(industryBrand.getIndustryId());
		industryBrand.setIndustryName(industry.getName());
		industryBrand.setCentreId(getUserCentreId());
		if (industryBrandService.saveIndustryBrand(industryBrand) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 移除操作
	 * @param id
	 * @return
	 */
	@Log(module = "品牌管理", operation = "移除商品类别")
	@ResponseBody
	@RequiresPermissions("centre:industryBrand:remove")
	@PostMapping("/remove")
	public ResultVo remove(Long id) {
		if (ValidateUtil.isEmptyObjectOrString(id)) {
			return ResultVo.error();
		}
		if (industryBrandService.removeProductGrade(id) < 1) {
			return ResultVo.error();
		}
		return ResultVo.ok();
	}

	/**
	 * 根据关联行业id获取对应品牌
	 *
	 * @param parentId
	 * @return
	 */
	@ResponseBody
	@PostMapping("/getIndustryBrandListByIndustryId")
	public ResultVo getIndustryBrandListByIndustryId(Long parentId) {
		if (parentId == null) {
			return ResultVo.error();
		}
		HashMap hashMap = new HashMap();
		hashMap.put("industryId", parentId);
		hashMap.put("centreId", getUserCentreId());
		return ResultVo.ok().put("industryBrandList", industryBrandService.listByCentreId(hashMap));
	}

}
